<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header_text">
        <img src="../assets/heima.png" alt="" />
        <span class="text">电商后台管理系统</span>
      </div>
      <el-button type="info" v-on:click="loginout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 列表区域 -->
      <el-aside :width="iscollapse? '64px' : '240px' ">
        <div class="toggle-button" v-on:click='toggleCollapse'>|||</div>
        <!-- 侧边栏区域 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse='iscollapse' :collapse-transition='false' router :default-active="activePath">
          <!-- 一级导航 -->
          <el-submenu :index="item.id + ''" v-for='item in menulist' :key="item.id">
            <!-- 一级导航的模板 -->
            <template slot="title">
              <!-- 一级导航的字体图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 一级导航的文本信息 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级导航 -->
            <el-menu-item  :index=" '/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
              <template slot="title">
                  <!-- 字体图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本信息 -->
                  <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
          
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist:[],
      iconsObj:{
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      iscollapse:false,
      // 被激活的新地址
      activePath:''
    }
  },
  created() { // 生命周期，钩子函数，data初始化完成
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted() {},
  methods: {
    // 退出功能
    loginout: function () {
      // 先清空token
      window.sessionStorage.clear()
      // 利用编程式导航，跳转到登录页面
      this.$router.push('/login')
    },
    // 获取左侧菜单的信息
    getMenuList: async function(){
      const {data:res} = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error('获取菜单列表失败！')
      this.menulist = res.data
      
    },
    toggleCollapse:function(){
      this.iscollapse = !this.iscollapse
    },
    saveNavState:function(activePath){
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
}
</script>

<style scoped lang="less">
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button{
  background-color :#4a5064 ;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

</style>
